package cn.matezk.cloud.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.matezk.cloud.beans.*;
import cn.matezk.cloud.model.Device;
import cn.matezk.cloud.service.IMiniAppService;
import cn.matezk.cloud.service.ITencentService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iot.v20180123.IotClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.CallDeviceActionSyncResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("miniApp")
public class DeviceController {
    @Autowired
    private IMiniAppService miniAppService;
    @Autowired
    private ITencentService tencentService;
    @Autowired
    private IotClient iotClient;
    @Value("${tencent.iot-app-id}")
    private String iotAppId;

    @PostMapping("wechatNotifyEnable")
    public Result wechatNotifyEnable(@RequestParam String unionId, @RequestParam String userId, @RequestParam Boolean wechatNotifyEnable) {
        return miniAppService.wechatNotifyEnable(unionId, userId, wechatNotifyEnable);
    }

    @GetMapping("wechatNotifyEnable")
    public Result wechatNotifyEnable(@RequestParam String unionId) {
        Boolean enable = miniAppService.wechatNotifyEnable(unionId);
        return Result.data(enable);
    }

    @GetMapping("device")
    public Result device(@RequestParam String deviceId) {
        Device device = miniAppService.detail(deviceId);
        return ObjectUtil.isNull(device) ? Result.error("设备不存在") : Result.data(device);
    }

    @PostMapping("updateDevice")
    public Result updateDevice(@RequestBody Device device) {
        miniAppService.updateDevice(device);
        return Result.ok();
    }

    @PostMapping("bindFamily")
    public Result bindFamily(@RequestBody BindDeviceRequest request) throws TencentCloudSDKException {
        miniAppService.bindFamily(request);
        return Result.ok();
    }

    @DeleteMapping("unbindDevice")
    public Result unbindDevice(@RequestParam String deviceId) throws TencentCloudSDKException {
        CallDeviceActionSyncResponse response = miniAppService.unbindDevice(deviceId);
        return Result.data(response);
    }

    @PostMapping("joinFamily")
    public Result joinFamily(@RequestParam String familyId, @RequestParam String userId) {
        miniAppService.joinFamily(familyId, userId);
        return Result.ok();
    }

    @DeleteMapping("removeFamily")
    public Result removeFamily(@RequestParam String familyId, @RequestParam String userId) {
        miniAppService.removeFamily(familyId, userId);
        return Result.ok();
    }

    @PostMapping("transferFamily")
    public Result transferFamily(@RequestParam String familyId, @RequestParam String fromUserId, @RequestParam String toUserId) {
        miniAppService.transferFamily(familyId, fromUserId, toUserId);
        return Result.ok();
    }

    @PostMapping("AppGetFamilyDeviceList")
    public Result appGetFamilyDeviceList(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appGetFamilyDeviceList(params);
        return Result.data(payload);
    }

    @PostMapping("AppListUserShareDevices")
    public Result appListUserShareDevices(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appListUserShareDevices(params);
        return Result.data(payload);
    }

    @PostMapping("AppDeleteDeviceInFamily")
    public Result appDeleteDeviceInFamily(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appDeleteDeviceInFamily(params);
        return Result.data(payload);
    }

    @PostMapping("AppBindUserShareDevice")
    public Result appBindUserShareDevice(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appBindUserShareDevice(params);
        return Result.data(payload);
    }

    @PostMapping("AppRemoveShareDeviceUser")
    public Result appRemoveShareDeviceUser(@RequestBody JSONObject params, HttpServletRequest request) {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appRemoveShareDeviceUser(params);
        return Result.data(payload);
    }

    @PostMapping("AppRemoveUserShareDevice")
    public Result appRemoveUserShareDevice(@RequestBody JSONObject params, HttpServletRequest request) {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appRemoveShareDeviceUser(params);
        return Result.data(payload);
    }

    @PostMapping("AppListShareDeviceUsers")
    public Result appListShareDeviceUsers(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appListShareDeviceUsers(params);
        return Result.data(payload);
    }

    @PostMapping("AppJoinFamily")
    public Result appJoinFamily(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appJoinFamily(params);
        return Result.data(payload);
    }

    @PostMapping("AppExitFamily")
    public Result appExitFamily(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appExitFamily(params);
        return Result.data(payload);
    }

    @PostMapping("AppDeleteFamilyMember")
    public Result appDeleteFamilyMember(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appDeleteFamilyMember(params);
        return Result.data(payload);
    }

    @PostMapping("AppTransferFamily")
    public Result appTransferFamily(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appTransferFamily(params);
        return Result.data(payload);
    }

    @PostMapping("AppUpdateDeviceInFamily")
    public Result appUpdateDeviceInFamily(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appUpdateDeviceInFamily(params);
        return Result.data(payload);
    }

    @PostMapping("AppModifyFamilyDeviceRoom")
    public Result appModifyFamilyDeviceRoom(@RequestBody JSONObject params, HttpServletRequest request) throws TencentCloudSDKException {
        params.set("Action", request.getRequestURI().split("/")[2]);
        JSONObject payload = tencentService.appModifyFamilyDeviceRoom(params);
        return Result.data(payload);
    }

    @PostMapping("SetLocation")
    public Result setLocation(@RequestBody Device device) {
        miniAppService.updateDevice(device);
        return Result.ok();
    }

    @PostMapping("transfer")
    public String transfer(@RequestBody CreateTransfer transfer) throws TencentCloudSDKException {
        return tencentService.sceneTransfer(transfer);
    }

    @PostMapping("agreeTransfer")
    public Boolean agreeTransfer(@RequestBody AgreeTransfer transfer) throws TencentCloudSDKException, InterruptedException {
        tencentService.agreeTransfer(transfer);
        return Boolean.TRUE;
    }

    @PostMapping("deviceSearch")
    public List<SimpleDevice> deviceSearch(@RequestBody DeviceSearch query) {
        query.setIotAppId(iotAppId);
        return miniAppService.deviceSearch(query);
    }

}
